﻿namespace PowerSong.UserInterface
    {
    using System;
    using System.Text;
    using System.Windows;
    using System.Collections.Generic;
    using PowerSong.SongDatabase;

    public static class Utilities
        {

        public static void ShowErrorMessage(string text)
            {
            ShowErrorMessage("Error", text);
            }

        public static void ShowErrorMessage(string title, string text)
            {
            MessageBox.Show(text, title, MessageBoxButton.OK, MessageBoxImage.Error);
            }

        public static void ShowErrorMessage(string title, string prependText, Exception additionalDetails)
            {
            var fullText = prependText + Environment.NewLine + Environment.NewLine + additionalDetails.Message;
            MessageBox.Show(fullText, title, MessageBoxButton.OK, MessageBoxImage.Error);
            }

        public static void ShowErrorMessage(string title, params string[] lines)
            {
            ShowMessage(MessageBoxImage.Error, title, lines);
            }

        public static void ShowInformationMessage(string title, params string[] lines)
            {
            ShowMessage(MessageBoxImage.Information, title, lines);
            }

        public static bool Confirm(string title, string text)
            {
            var result = MessageBox.Show(text, title, MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
            return result == MessageBoxResult.Yes;
            }

        private static void ShowMessage(MessageBoxImage image, string title, params string[] lines)
            {

            // Compose the final message, consisting of all the given lines
            var text = new StringBuilder();
            var firstLine = true;
            foreach (var line in lines)
                {
                if (!firstLine) text.AppendLine();
                text.AppendLine(line);                
                firstLine = false;
                }

            // Actually show the message
            MessageBox.Show(text.ToString(), title, MessageBoxButton.OK, image);

            }

        public static string[] TextToVerses(string versesText)
            {

            var result = new List<string>();
            var verses = versesText.Split(new string[] { Environment.NewLine + Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var verseText in verses)
                {
                var actualVerseText = verseText.Trim();
                if (!string.IsNullOrEmpty(actualVerseText)) result.Add(actualVerseText);
                }
            return result.ToArray();

            }

        public static string VersesToText(IEnumerable<string> verses)
            {
            var result = new StringBuilder();
            var firstVerse = true;
            foreach (var verseText in verses)
                {
                if (!firstVerse) result.AppendLine().AppendLine();
                result.Append(verseText);
                firstVerse = false;
                }
            return result.ToString();
            }

        public static Dictionary<T, string> CreateList<T>() where T : struct
            {
            var result = new Dictionary<T, string>();
            foreach (T value in Enum.GetValues(typeof(T)))
                {

                var info = typeof(T).GetField(value.ToString());
                var attributes = (DescriptionAttribute[])info.GetCustomAttributes(typeof(DescriptionAttribute), false);
                var description = (attributes != null && attributes.Length > 0) ? attributes[0].Description : value.ToString();
                result[value] = description;

                }
            return result;
            }

        }

    }
